package com.example.zhu95.mytimber.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.activity.MainActivity;
import com.example.zhu95.mytimber.adapters.ArtistGridAdapter;
import com.example.zhu95.mytimber.adapters.ArtistLinearAdapter;
import com.example.zhu95.mytimber.entity.ArtistBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.listener.ArtistClickListener;
import com.example.zhu95.mytimber.ui.ArtistsSectionTitleIndicator;
import com.example.zhu95.mytimber.utils.MyComparator;

import java.util.Collections;
import java.util.List;

import xyz.danoz.recyclerviewfastscroller.vertical.VerticalRecyclerViewFastScroller;

import static com.example.zhu95.mytimber.ConstantValue.CURRENT_VIEW_MODE;
import static com.example.zhu95.mytimber.ConstantValue.GRID_VIEW;
import static com.example.zhu95.mytimber.ConstantValue.LINEAR_VIEW;

/**
 * @author zhu95
 */
public class ArtistFragment extends Fragment {
    int currentViewMode = GRID_VIEW;
    ArtistClickListener artistClickListener;
    RecyclerView recyclerViewArtists;
    ArtistGridAdapter artistGridAdapter = null;
    GridLayoutManager gridLayoutManager;
    ArtistLinearAdapter artistLinearAdapter = null;
    LinearLayoutManager linearLayoutManager;
    VerticalRecyclerViewFastScroller scroller;
    ArtistsSectionTitleIndicator indicator;
    List<ArtistBean> artists = SongData.artists;

    public List<ArtistBean> getArtists() {
        return artists;
    }

    public void setArtists(List<ArtistBean> artists) {
        this.artists = artists;
        notifyDataSetChanged();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivity mainActivity = (MainActivity) getActivity();
        artistClickListener = mainActivity.getArtistClickListener();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_library_artist, container, false);
        Context context = view.getContext();
        recyclerViewArtists = view.findViewById(R.id.recycler_view_artist);
        scroller = view.findViewById(R.id.scroller_artist);
        indicator = view.findViewById(R.id.indicator_artist);
        if (artistGridAdapter == null) {
            artistGridAdapter = new ArtistGridAdapter(artists, artistClickListener);
        }
        gridLayoutManager = new GridLayoutManager(context, 2);
        if (artistLinearAdapter == null) {
            artistLinearAdapter = new ArtistLinearAdapter(artists, artistClickListener);
        }
        linearLayoutManager = new LinearLayoutManager(context);
        scroller.setRecyclerView(recyclerViewArtists);
        recyclerViewArtists.setOnScrollListener(scroller.getOnScrollListener());
        scroller.setSectionIndicator(indicator);
        setRecyclerViewMode(currentViewMode);
        return view;
    }

    public void setRecyclerViewMode(int viewMode) {
        if (viewMode == LINEAR_VIEW) {
            scroller.setVisibility(View.VISIBLE);
            indicator.setVisibility(View.VISIBLE);
            currentViewMode = viewMode;
            recyclerViewArtists.setAdapter(artistLinearAdapter);
            recyclerViewArtists.setLayoutManager(linearLayoutManager);
        } else if (viewMode == GRID_VIEW) {
            scroller.setVisibility(View.GONE);
            indicator.setVisibility(View.GONE);
            currentViewMode = viewMode;
            recyclerViewArtists.setAdapter(artistGridAdapter);
            recyclerViewArtists.setLayoutManager(gridLayoutManager);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle args = getArguments();
        if (args != null) {
            int viewMode = args.getInt(CURRENT_VIEW_MODE);
            if (currentViewMode == viewMode) {
                return;
            }
            setRecyclerViewMode(viewMode);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Bundle args = new Bundle();
        args.putInt(CURRENT_VIEW_MODE, currentViewMode);
        setArguments(args);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        try {
            switch (item.getItemId()) {
                case R.id.sort_az:
                    Collections.sort(artists, new MyComparator(ArtistBean.class.getDeclaredField("artist")));
                    break;
                case R.id.sort_za:
                    Collections.sort(artists, new MyComparator(ArtistBean.class.getDeclaredField("artist"), true));
                    break;
                case R.id.sort_songnum:
                    Collections.sort(artists, new MyComparator(ArtistBean.class.getDeclaredField("numOfSong")));
                    break;
                case R.id.sort_albumnum:
                    Collections.sort(artists, new MyComparator(ArtistBean.class.getDeclaredField("numOfAlbum"), true));
                    break;
                default:
            }
            // notifyDataSetChanged();
            artistLinearAdapter.notifyDataSetChanged();
            artistGridAdapter.notifyDataSetChanged();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return super.onOptionsItemSelected(item);
    }


    public void notifyDataSetChanged() {
        artistLinearAdapter.setArtists(artists);
        artistLinearAdapter.notifyDataSetChanged();
        artistGridAdapter.setArtists(artists);
        artistGridAdapter.notifyDataSetChanged();
    }
}
